package com.recharge.web.controller;


import com.recharge.domain.vo.GoodsMerchantDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.recharge.common.annotation.OperationLogDetail;
import com.recharge.common.domain.PageDO;
import com.recharge.common.enums.OperationLogType;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.GoodsProviderQueryDO;
import com.recharge.domain.vo.GoodsProviderDO;
import com.recharge.service.GoodsProviderService;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 商品与供应商关联 前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/goods-provider")
public class GoodsProviderController extends BaseController {

	@Autowired
    private GoodsProviderService goodsProviderService;

	@RequiresPermissions("goods-provider:page")
	@RequestMapping(value="/page.html", method = RequestMethod.POST)
	@ApiOperation(value="分页查询供货商品组关联", notes="")
	public @ResponseBody  ObjectResponse<PageDO> page(@RequestBody GoodsProviderQueryDO query) {

	 	ObjectResponse<PageDO> response = new ObjectResponse<>();

	 	ResultDO result = goodsProviderService.find(query);

	 	if(result.isSuccess()) {
			response.setData((PageDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}

	 	return response;
	 }

	@RequiresPermissions("goods-provider:otherPage")
	@RequestMapping(value="/otherPage.html", method = RequestMethod.POST)
	@ApiOperation(value="分页查询供货商品关联", notes="")
	public @ResponseBody  ObjectResponse<PageDO> otherPage(@RequestBody GoodsProviderQueryDO query) {

		ObjectResponse<PageDO> response = new ObjectResponse<>();

		ResultDO result = goodsProviderService.otherPage(query);

		if(result.isSuccess()) {
			response.setData((PageDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}

		return response;
	}


	@RequiresPermissions("goods-provider:add")
	@OperationLogDetail(detail = "添加供货商品关联", operationType = OperationLogType.ADD)
	@RequestMapping(value="/add.html", method = RequestMethod.POST)
	@ApiOperation(value="添加供货商品关联", notes="")
	public @ResponseBody  ObjectResponse<Object> add(@RequestBody GoodsProviderDO param) {
	 	ObjectResponse<Object> response = new ObjectResponse<>();
	 	
	 	param.setModifier(getUserName());
		param.setCreator(getUserName());
	 	
	 	ResultDO result = goodsProviderService.addBatch(param);
	 	if(result.isSuccess()) {
			response.setData(null);
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}
	 	return response;
	 }


	@RequiresPermissions("goods-provider:delete")
	@OperationLogDetail(detail = "删除供货商品关联", operationType = OperationLogType.DELETE)
	@RequestMapping(value="/delete.html", method = RequestMethod.POST)
	@ApiOperation(value="删除供货商品关联", notes="")
	public @ResponseBody  ObjectResponse<Object> delete(@RequestBody GoodsProviderDO param) {
	 	ObjectResponse<Object> response = new ObjectResponse<>();
	 	
	 	ResultDO result = goodsProviderService.remove(param.getProviderId(), param.getGoodsId(), getUserName());
	 	if(result.isSuccess()) {
			response.setData(null);
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}
	 	return response;
	 }

	@RequiresPermissions("goods-provider:deleteBatch")
	@OperationLogDetail(detail = "批量删除供货商品关联", operationType = OperationLogType.DELETE)
	@RequestMapping(value="/deleteBatch.html", method = RequestMethod.POST)
	@ApiOperation(value="批量删除供货商品关联", notes="")
	public @ResponseBody  ObjectResponse<Object> deleteBatch(@RequestBody GoodsProviderDO param) {
		ObjectResponse<Object> response = new ObjectResponse<>();

		ResultDO result = goodsProviderService.removeBatch(param.getProviderId(), param.getGoodsIds(), getUserName());
		if(result.isSuccess()) {
			response.setData(null);
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}
		return response;
	}

	@RequiresPermissions("goods-provider:update")
	@OperationLogDetail(detail = "修改供货商品关联", operationType = OperationLogType.UPDATE)
	@RequestMapping(value="/update.html", method = RequestMethod.POST)
	@ApiOperation(value="修改供货商品关联", notes="")
	public @ResponseBody  ObjectResponse<Object> update(@RequestBody GoodsProviderDO param) {
	 	ObjectResponse<Object> response = new ObjectResponse<>();
	 	param.setModifier(getUserName());
	 	ResultDO result = goodsProviderService.edit(param);
	 	if(result.isSuccess()) {
			response.setData(null);
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}
	 	return response;
	 }


	@RequiresPermissions("goods-provider:updateStatus")
	@RequestMapping(value="/updateStatus.html", method = RequestMethod.POST)
	@ApiOperation(value="供货商品组维护", notes="")
	public @ResponseBody
	ObjectResponse updateStatus(@RequestBody GoodsProviderDO domain) {

		ObjectResponse response = new ObjectResponse();


		ResultDO result = goodsProviderService.updateStatus(domain.getProviderId(), domain.getGoodsIds(), domain.getStatus(), getUserName());

		if(!result.isSuccess()) {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}
		return response;
	}

	@RequiresPermissions("goods-provider:updateCostDiscountBatch")
	@RequestMapping(value="/updateCostDiscountBatch.html", method = RequestMethod.POST)
	@ApiOperation(value="批量修改成本折扣", notes="")
	public @ResponseBody  ObjectResponse<Object> updateCostDiscountBatch(@RequestBody GoodsProviderDO param) {
		ObjectResponse<Object> response = new ObjectResponse<>();
		ResultDO result = goodsProviderService.updateCostDiscountBatch(param.getGroupId(), param.getProviderId(), param.getCostDiscount(), param.getPriority(), getUserName());
		if(result.isSuccess()) {
			response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(result.getErrorCode().getCode());
			response.setMsg(result.getErrorCode().getMsg());
		}
		return response;
	}
	
}
